import itertools

pets = [
    {'name': 'Max', 'species': 'dog'},
    {'name': 'Lucy', 'species': 'cat'},
    {'name': 'Buddy', 'species': 'dog'},
    {'name': 'Molly', 'species': 'cat'},
    {'name': 'Charlie', 'species': 'dog'}
]

# 按照species字段进行分组
grouped_pets = itertools.groupby(pets, key=lambda x: x['species'])

for key, group in grouped_pets:
    print(key)
    for pet in group:
        print(pet)